﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

namespace NCKH.Admin
{
    public partial class Linhvuc : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                hienlinhvuc();
                // hienloaihinh();
            }
        }

        protected void hienlinhvuc()
        {
            using (DataTable LOAILINHVUC = getLinhvuc())
            {
                grvlinhvuc.DataSource = LOAILINHVUC;
                grvlinhvuc.DataBind();
            }
        }
        DataTable getLinhvuc()
        {
            string sqlSelect = "SELECT MaLV,TenLV  FROM LOAILINHVUC";
            string connectionString = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            using (SqlConnection cnn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = new SqlCommand(sqlSelect, cnn))
                {
                    using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                    {
                        DataTable tbl = new DataTable("LOAILINHVUC");
                        da.Fill(tbl);
                        return tbl;
                    }
                }
            }
        }

        protected void ibtThem_Click(object sender, EventArgs e)
        {
            if (kiemtralinhvuc() == false)
            {
                Themlinhvuc();
            }
        }
        protected void Themlinhvuc()
        {
            string connectionString = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            using (SqlConnection cnn = new SqlConnection(connectionString))
            using (SqlCommand cmd = new SqlCommand("Themlinhvuc", cnn))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@TenLV", txtLV.Text);
                cnn.Open();
                cmd.ExecuteNonQuery();
                cnn.Close();
                hienlinhvuc();
            }
        }
        protected bool kiemtralinhvuc()
        {
            string connectionString = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            string selectLV = "Select TenLV from LOAILINHVUC where TenLV like N'%" + txtLV.Text + "%'";
            using (SqlConnection cnn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = new SqlCommand(selectLV, cnn))
                {
                    cmd.CommandType = CommandType.Text;
                    //cmd.Parameters.AddWithValue("@TenLV", txtLV.Text);
                    cnn.Open();
                    using (SqlDataReader rd = cmd.ExecuteReader())
                    {
                        if (rd.Read())
                        {
                            Response.Write("<script>alert('Đã tồn tại tên lĩnh vực')</script>");
                            return true;
                        }
                        else
                        {
                            Response.Write("<script>alert('Thêm thành công!')</script>");
                            return false;
                        }
                        rd.Close();
                    }
                    cnn.Close();
                }
            }
        }
        protected void grvLinhvuc_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.ToLower().Equals("xoa"))
            {
                int linhvucID = Convert.ToInt32(e.CommandArgument);
                string connectionString = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
                using (SqlConnection Cnn = new SqlConnection(connectionString))
                {
                    using (SqlCommand Cmd = new SqlCommand("Xoalinhvuc", Cnn))
                    {
                        Cmd.CommandType = CommandType.StoredProcedure;
                        Cmd.Parameters.AddWithValue("@MaLV", linhvucID);
                        Cnn.Open();
                        Cmd.ExecuteNonQuery();
                        Cnn.Close();
                        hienlinhvuc();
                    }
                }
            }
        }
        protected void delete_linhvuc(int lvucID)
        {
            // if (e.CommandName.ToLower().Equals("xoa"))
            //{
            // int chuyenmucID = Convert.ToInt32(e.CommandArgument);
            string connectionString = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            using (SqlConnection Cnn = new SqlConnection(connectionString))
            {
                using (SqlCommand Cmd = new SqlCommand("Xoalinhvuc", Cnn))
                {
                    Cmd.CommandType = CommandType.StoredProcedure;
                    Cmd.Parameters.AddWithValue("@MaLV", lvucID);
                    Cnn.Open();
                    Cmd.ExecuteNonQuery();
                    Cnn.Close();

                }
            }
            // }
        }
        protected void ibtXoa_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow r in grvlinhvuc.Rows)
            {
                CheckBox cb = r.FindControl("chkchon") as CheckBox;
                if (cb != null && cb.Checked)
                {
                    int lvucID = Convert.ToInt32(grvlinhvuc.DataKeys[r.RowIndex].Value);
                    delete_linhvuc(lvucID);
                }
            }
            hienlinhvuc();
        }
        protected void grvlinhvuc_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grvlinhvuc.PageIndex = e.NewPageIndex;
            hienlinhvuc();
            // btnXem_Click(sender, e);
        }
        protected void ibtHuy_Click(object sender, EventArgs e)
        {
            txtLV.Text =
            string.Empty;
            txtLV.Focus();
        }

        protected void ibtTimkiem_Click(object sender, EventArgs e)
        {

            using (DataTable LOAILINHVUC = hienthongtintimkiem())
            {
                grvlinhvuc.DataSource = LOAILINHVUC;
                grvlinhvuc.DataBind();
            }

        }
        private DataTable hienthongtintimkiem()
        {

            string connectionString = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            using (SqlConnection Cnn = new SqlConnection(connectionString))
            {
                using (SqlCommand Cmd = new SqlCommand("timlinhcuc", Cnn))
                {
                    Cmd.CommandType = CommandType.StoredProcedure;
                    Cmd.Parameters.AddWithValue("@ten", '%' + txtTimkiem.Text + '%');
                    using (SqlDataAdapter da = new SqlDataAdapter(Cmd))
                    {
                        da.SelectCommand = Cmd;
                        DataTable tb = new DataTable("LOAILINHVUC");
                        da.Fill(tb);
                        return tb;
                    }
                }
            }
        }
    }
}